﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for EmployeeBLL
/// </summary>

public class EmployeeBLL : IEmployeeBLL
{
    //inject DAL
    private IEmployeeDAL EmpDAL = Factory<IEmployeeDAL>.CreateInstance();

    public EmployeeBLL()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    /// <summary>
    /// GetAll
    /// </summary>
    /// <returns>List<Employee></returns>
    public List<Employee> GetAll()
    {
        try
        {
            return EmpDAL.GetAll();
        }
        catch (Exception)
        {
            throw;
        }
    }    

    /// <summary>
    /// Search
    /// </summary>
    /// <param name="emp">Employee</param>
    /// <returns>List<Employee></returns>
    public List<Employee> Search(Employee emp)
    {
        try
        {
            return EmpDAL.Search(emp);
        }
        catch (Exception)
        {
            throw;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="id">Guid</param>
    /// <returns>Employee</returns>
    public Employee GetById(Guid id)
    {
        try
        {
            return EmpDAL.GetById(id);
        }
        catch (Exception)
        {
            throw;
        }
    }

    /// <summary>
    /// CheckLogin
    /// </summary>
    /// <param name="emp">Employee</param>
    /// <returns>Employee</returns>
    public Employee CheckLogin(Employee emp)
    {
        try
        {
            //Encrypt passsword
            emp.Password = Utils.md5Encrypt(emp.Password);

            //init BO
            Employee temp = new Employee();
            temp=EmpDAL.CheckLogin(emp);

            //Update login date
            if (temp.Id!=Guid.Empty)
            {
                EmpDAL.UpdateLogin(temp.Id);
            }

            return temp;
        }
        catch (Exception)
        {
            throw;
        }
    }

    /// <summary>
    /// InsertOrUpdate
    /// </summary>
    /// <param name="emp">emp</param>
    /// <returns>Employee</returns>
    public bool InsertOrUpdate(Employee emp)
    {
        try
        {
            //encrypt password
            emp.Password = Utils.md5Encrypt(emp.Password);

            return EmpDAL.InsertOrUpdate(emp);
        }
        catch (Exception)
        {
            throw;
        }
    }

    /// <summary>
    /// Delete
    /// </summary>
    /// <param name="id">Guid</param>
    /// <returns>bool</returns>
    public bool Delete(Guid id)
    {
        try
        {
            return EmpDAL.Delete(id);
        }
        catch (Exception)
        {
            throw;
        }
    }

    /// <summary>
    /// Search
    /// </summary>
    /// <param name="keyword">keyword</param>
    /// <returns>bool</returns>
    public bool Search(string keyword)
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// ActiveAction
    /// </summary>
    /// <param name="emp">Employee</param>
    /// <returns>bool</returns>
    public bool ActiveAction(Employee emp)
    {
        try
        {
            //switch active
            if (emp.Active)
                emp.Active = false;
            else
                emp.Active = true;     
            
            return EmpDAL.ActiveAction(emp);
        }
        catch (Exception)
        {
            throw;
        }
    }

    /// <summary>
    /// CheckIsCodeExist
    /// </summary>
    /// <param name="code">string</param>
    /// <returns>bool</returns>
    public bool CheckIsCodeExist(string code)
    {
        try
        {
            return EmpDAL.CheckIsCodeExist(code);
        }
        catch (Exception)
        {
            throw;
        }
    }

    /// <summary>
    /// GetMaxEmpCode
    /// </summary>
    /// <param name="code">string</param>
    /// <returns>bool</returns>
    public string GetMaxEmpCode()
    {
        try
        {
            return EmpDAL.GetMaxEmpCode();
        }
        catch (Exception)
        {
            throw;
        }
    }

    /// <summary>
    /// CheckUserIsExist
    /// </summary>
    /// <param name="code">string</param>
    /// <returns>bool</returns>
    public bool CheckUserIsExist(string user)
    {
        try
        {
            return EmpDAL.CheckUserIsExist(user);
        }
        catch (Exception)
        {
            throw;
        }
    }

    public bool CheckExistCodeInForeignKey(Guid id)
    {
        try
        {
            return EmpDAL.CheckExistCodeInForeignKey(id);
        }
        catch (Exception)
        {
            throw;
        }
    }

    public bool CheckOldPassWord(Employee emp)
    {
        try
        {
            //encrypt password
            emp.Password = Utils.md5Encrypt(emp.Password);
            return EmpDAL.CheckOldPassWord(emp);
        }
        catch (Exception)
        {
            throw;
        }
    }

    public bool ChangePassword(Employee emp)
    {
        try
        {
            //encrypt password
            emp.Password = Utils.md5Encrypt(emp.Password);
            return EmpDAL.ChangePassword(emp);
        }
        catch (Exception)
        {
            throw;
        }
    }

    public List<Employee> SearchEmplyeeAndTimeTracking(Employee emp, Working work)
    {
        try
        {
            return EmpDAL.SearchEmplyeeAndTimeTracking(emp,work);
        }
        catch (Exception)
        {
            throw;
        }
    }

    public List<Working> GetWorkingByEmployee(Guid employeeID, Working working)
    {
        try
        {
            return EmpDAL.GetWorkingByEmployee(employeeID, working);
        }
        catch (Exception)
        {
            throw;
        }
    }

    
    
}

